home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 051-075 / disk_068 / mg1b / paragraph.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  8KB  |  269 lines

  1. /*
  2.  * Code for dealing with paragraphs and filling. Adapted from MicroEMACS 3.6
  3.  * and GNU-ified by mwm@ucbvax.  Several bug fixes by blarson@usc-oberon.
  4.  */
  5. #include "def.h"
  6.  
  7. static int    fillcol = 70 ;
  8. #define    MAXWORD    256
  9.  
  10. /*
  11.  * go back to the begining of the current paragraph
  12.  * here we look for a <NL><NL> or <NL><TAB> or <NL><SPACE>
  13.  * combination to delimit the begining of a paragraph
  14.  */
  15. /*ARGSUSED*/
  16. gotobop(f, n, k) {
  17.     register int suc;    /* success of last backchar */
  18.  
  19.     if (n < 0)    /* the other way...*/
  20.         return(gotoeop(f, -n, KRANDOM));
  21.  
  22.     while (n-- > 0) {    /* for each one asked for */
  23.  
  24.         /* first scan back until we are in a word */
  25.         suc = backchar(FALSE, 1, KRANDOM);
  26.         while (!inword() && suc)
  27.             suc = backchar(FALSE, 1, KRANDOM);
  28.         curwp->w_doto = 0;    /* and go to the B-O-Line */
  29.  
  30.         /* and scan back until we hit a <NL><SP> <NL><TAB> or <NL><NL> */
  31.         while (lback(curwp->w_dotp) != curbp->b_linep)
  32.             if (llength(lback(curwp->w_dotp)) 
  33.                 && lgetc(curwp->w_dotp,0) != ' '
  34.                 && lgetc(curwp->w_dotp,0) != '\t')
  35.                 curwp->w_dotp = lback(curwp->w_dotp);
  36.             else
  37.                 break;
  38.     }
  39.     curwp->w_flag |= WFMOVE;    /* force screen update */
  40.     return TRUE;
  41. }
  42.  
  43. /*
  44.  * go forword to the end of the current paragraph
  45.  * here we look for a <NL><NL> or <NL><TAB> or <NL><SPACE>
  46.  * combination to delimit the begining of a paragraph
  47.  */
  48. /*ARGSUSED*/
  49. gotoeop(f, n, k) {
  50.     register int suc;    /* success of last backchar */
  51.  
  52.     if (n < 0)    /* the other way...*/
  53.         return(gotobop(f, -n, KRANDOM));
  54.  
  55.     while (n-- > 0) {    /* for each one asked for */
  56.  
  57.         /* Find the first word on/after the current line */
  58.         curwp->w_doto = 0;
  59.         suc = forwchar(FALSE, 1, KRANDOM);
  60.         while (!inword() && suc)
  61.             suc = forwchar(FALSE, 1, KRANDOM);
  62.         curwp->w_doto = 0;
  63.         if (curwp->w_dotp == curbp->b_linep)
  64.             break;                /* check for eob */
  65.         curwp->w_dotp = lforw(curwp->w_dotp);
  66.  
  67.         /* and scan forword until we hit a <NL><SP> or ... */
  68.         while (curwp->w_dotp != curbp->b_linep) {
  69.             if (llength(curwp->w_dotp)
  70.                 && lgetc(curwp->w_dotp,0) != ' '
  71.                 && lgetc(curwp->w_dotp,0) != '\t')
  72.                 curwp->w_dotp = lforw(curwp->w_dotp);
  73.             else
  74.                 break;
  75.         }
  76.     }
  77.     curwp->w_flag |= WFMOVE;    /* force screen update */
  78.     return TRUE;
  79. }
  80.  
  81. /*
  82.  * Fill the current paragraph according to the current
  83.  * fill column
  84.  */
  85. /*ARGSUSED*/
  86. fillpara(f, n, k) {
  87.     register int    c;        /* current char durring scan    */
  88.     register int    wordlen;    /* length of current word    */
  89.     register int    clength;    /* position on line during fill    */
  90.     register int    i;        /* index during word copy    */
  91.     register int    eopflag;    /* Are we at the End-Of-Paragraph? */
  92.     int         firstflag;    /* first word? (needs no space)    */
  93.     int        newlength;    /* tentative new line length    */
  94.     int        eolflag;    /* was at end of line        */
  95.     LINE         *eopline;    /* pointer to line just past EOP */
  96.     char wbuf[MAXWORD];        /* buffer for current word    */
  97.  
  98.     /* record the pointer to the line just past the EOP */
  99.     (VOID) gotoeop(FALSE, 1, KRANDOM);
  100.     eopline = curwp->w_dotp;
  101.  
  102.     /* and back top the begining of the paragraph */
  103.     (VOID) gotobop(FALSE, 1, KRANDOM);
  104.  
  105.     /* initialize various info */
  106.     i = TRUE;
  107.     while (i == TRUE && !inword())
  108.         i = forwchar(FALSE, 1, KRANDOM);
  109.     clength = curwp->w_doto;
  110.     wordlen = 0;
  111.  
  112.     /* scan through lines, filling words */
  113.     firstflag = TRUE;
  114.     eopflag = FALSE;
  115.     while (!eopflag) {
  116.         /* get the next character in the paragraph */
  117.         if (eolflag=(curwp->w_doto == llength(curwp->w_dotp))) {
  118.             c = ' ';
  119.             if (lforw(curwp->w_dotp) == eopline)
  120.                 eopflag = TRUE;
  121.         } else
  122.             c = lgetc(curwp->w_dotp, curwp->w_doto);
  123.  
  124.         /* and then delete it */
  125.         if (ldelete((RSIZE) 1, KNONE) == FALSE) return FALSE ;
  126.  
  127.         /* if not a separator, just add it in */
  128.         if (c != ' ' && c != '\t') {
  129.             if (wordlen < MAXWORD - 1)
  130.                 wbuf[wordlen++] = c;
  131.             else {
  132.                 /* You loose chars beyond MAXWORD if the word
  133.                  * is to long. I'm to lazy to fix it now; it
  134.                  * just silently truncated the word before, so
  135.                  * I get to feel smug.
  136.                  */
  137.                 ewprintf("Word too long!");
  138.             }
  139.         } else if (wordlen) {
  140.             /* calculate tenatitive new length with word added */
  141.             newlength = clength + 1 + wordlen;
  142.             /* if at end of line or at doublespace and previous
  143.              * character was one of '.','?','!' doublespace here.
  144.              */
  145.             if((eolflag || curwp->w_doto==llength(curwp->w_dotp)
  146.                 || (c=lgetc(curwp->w_dotp,curwp->w_doto))==' '
  147.                 || c=='\t')
  148.               && ISEOSP(wbuf[wordlen-1])
  149.               && wordlen<MAXWORD-1)
  150.                 wbuf[wordlen++] = ' ';
  151.             /* at a word break with a word waiting */
  152.              if (newlength <= fillcol) {
  153.                 /* add word to current line */
  154.                 if (!firstflag) {
  155.                     (VOID) linsert((RSIZE) 1, ' ');
  156.                     ++clength;
  157.                 }
  158.                 firstflag = FALSE;
  159.             } else {
  160.                 if(curwp->w_doto > 0 &&
  161.                     lgetc(curwp->w_dotp,curwp->w_doto-1)==' ') {
  162.                         curwp->w_doto -= 1;
  163.                     (VOID) ldelete((RSIZE) 1, KNONE);
  164.                 }
  165.                 /* start a new line */
  166.                 (VOID) lnewline();
  167.                 clength = 0;
  168.             }
  169.  
  170.             /* and add the word in in either case */
  171.             for (i=0; i<wordlen; i++) {
  172.                 (VOID) linsert((RSIZE) 1, wbuf[i]);
  173.                 ++clength;
  174.             }
  175.             wordlen = 0;
  176.         }
  177.     }
  178.     /* and add a last newline for the end of our new paragraph */
  179.     (VOID) lnewline();
  180.     /* we realy should wind up where we started, (which is hard to keep
  181.      * track of) but I think the end of the last line is better than the
  182.      * begining of the blank line.     */
  183.     (VOID) backchar(FALSE, 1, KRANDOM);
  184.     return TRUE;
  185. }
  186.  
  187. /* delete n paragraphs starting with the current one */
  188. /*ARGSUSED*/
  189. killpara(f, n, k) {
  190.     register int status;    /* returned status of functions */
  191.  
  192.     while (n--) {        /* for each paragraph to delete */
  193.  
  194.         /* mark out the end and begining of the para to delete */
  195.         (VOID) gotoeop(FALSE, 1, KRANDOM);
  196.  
  197.         /* set the mark here */
  198.             curwp->w_markp = curwp->w_dotp;
  199.             curwp->w_marko = curwp->w_doto;
  200.  
  201.         /* go to the begining of the paragraph */
  202.         (VOID) gotobop(FALSE, 1, KRANDOM);
  203.         curwp->w_doto = 0;    /* force us to the begining of line */
  204.     
  205.         /* and delete it */
  206.         if ((status = killregion(FALSE, 1, KRANDOM)) != TRUE)
  207.             return(status);
  208.  
  209.         /* and clean up the 2 extra lines */
  210.         (VOID) ldelete((RSIZE) 1, KFORW);
  211.     }
  212.     return(TRUE);
  213. }
  214.  
  215. /*
  216.  * check to see if we're past fillcol, and if so,
  217.  * justify this line. As a last step, justify the line.
  218.  */
  219. /*ARGSUSED*/
  220. fillword(f, n, k) {
  221.     register char    c;
  222.     register int    col, i, nce;
  223.  
  224.     for (i = col = 0; col <= fillcol; ++i, ++col) {
  225.         if (i == curwp->w_doto) return selfinsert(f, n, k) ;
  226.         c = lgetc(curwp->w_dotp, i);
  227.         if (
  228. #ifdef    NOTAB
  229.             !(mode&MNOTAB) &&
  230. #endif
  231.             c == '\t') col |= 0x07;
  232.         else if (ISCTRL(c) != FALSE) ++col;
  233.     }
  234.     if (curwp->w_doto != llength(curwp->w_dotp)) {
  235.         (VOID) selfinsert(f, n, k);
  236.         nce = llength(curwp->w_dotp) - curwp->w_doto;
  237.     } else nce = 0;
  238.     curwp->w_doto = i;
  239.  
  240.     if ((c = lgetc(curwp->w_dotp, curwp->w_doto)) != ' ' && c != '\t')
  241.         do {
  242.             (VOID) backchar(FALSE, 1, KRANDOM);
  243.         } while ((c = lgetc(curwp->w_dotp, curwp->w_doto)) != ' '
  244.               && c != '\t' && curwp->w_doto > 0);
  245.  
  246.     if (curwp->w_doto == 0)
  247.         do {
  248.             (VOID) forwchar(FALSE, 1, KRANDOM);
  249.         } while ((c = lgetc(curwp->w_dotp, curwp->w_doto)) != ' '
  250.               && c != '\t' && curwp->w_doto < llength(curwp->w_dotp));
  251.  
  252.     (VOID) delwhite(FALSE, 1, KRANDOM);
  253.     (VOID) lnewline();
  254.     curwp->w_doto = llength(curwp->w_dotp) - nce;
  255.     curwp->w_flag |= WFMOVE;
  256.     if (nce == 0 && curwp->w_doto != 0) return fillword(f, n, k);
  257.     return TRUE;
  258. }
  259.  
  260. /* Set fill column to n. */
  261. /*ARGSUSED*/
  262. setfillcol(f, n, k) {
  263.     extern int    getcolpos() ;
  264.  
  265.     fillcol = ((f == FALSE) ? getcolpos() : n);
  266.     if (kbdmop == NULL) ewprintf("Fill column set to %d", fillcol);
  267.         return(TRUE);
  268. }
  269.